/* Copyright (C) 2010 MoSync AB

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License, version 2, as published by
the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with this program; see the file COPYING.  If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
#include "GridMenu.h"
#include <conprint.h>

GridMenu::GridMenu(int x, int y, int width, int height, Widget *parent, int rows, int columns)
         : Layout(x, y, width, height, parent, rows, columns)
{
	////lprintfln("Inside menu constructor");
	origW = width;
	itemCount = 0;
	selectedIndex = -1;
	selectedWidget = NULL;
	listener = NULL;
	enabled = false;
	_rows = rows;
	_cols = columns;

	//set all the panels to active
	for(int i = 0; i < _rows * _cols; i++)
	{
		activePanels.add(1);
	}

	this->setPaddingLeft((width - this->getWidth()) / 2);
	this->setPaddingRight(5);
	this->setBackgroundColor(0xFF0000);
	this->setDrawBackground(true);
	
	////lprintfln("Menu constructed");
}

GridMenu::~GridMenu()
{
	if(enabled)
	{
		Environment::getEnvironment().removeKeyListener(this);
		Environment::getEnvironment().removePointerListener(this);
	}
}

void GridMenu::drawWidget()
{
}

void GridMenu::add(Widget *button)
{
	if(button != NULL && itemCount < _rows * _cols)
	{
		Layout::add(button);	
		itemCount++;
	}
	////lprintfln("button added");
}

void GridMenu::addListener(GridMenuListener *l)
{
	listener = l;
}

void GridMenu::pointerPressEvent(MAPoint2d point)
{
	locateWidget(point, false);
}

void GridMenu::pointerMoveEvent(MAPoint2d point)
{
	locateWidget(point, false);
}

void GridMenu::pointerReleaseEvent(MAPoint2d point)
{
	locateWidget(point, true);
}

void GridMenu::informListener()
{
	if(listener != NULL)
		listener->GridMenuItemSelected(selectedIndex, selectedWidget);
}

void GridMenu::selectButton(int index)
{
	////lprintfln("Selecting button %d", index);
	if(index <= itemCount && index > -1)
	{
		////lprintfln("Testing previously selected widget");
		if(selectedWidget != NULL)
		{
			selectedWidget->setSelected(false);
			previouslySelectedWidget = selectedWidget;
		}
		selectedWidget = getChildren()[index];
		////lprintfln("Selecting new button");
		selectedWidget->setSelected(true);
		////lprintfln("Updating index");
		selectedIndex = index;
	}
	requestRepaint();
}

void GridMenu::selectNext()
{
	////lprintfln("Current index: %d", selectedIndex);
	int nextItem = (selectedIndex + 1) % itemCount;
	////lprintfln("First option: %d", nextItem);
	while(activePanels[nextItem] == 0)
	{
		nextItem = (nextItem + 1) % itemCount;
		////lprintfln("Hide the next one and try %d", nextItem);
	}

	selectButton(nextItem);
}

void GridMenu::selectPrevious()
{
	////lprintfln("Selecting previous");
	////lprintfln("selectedIndex: %d", selectedIndex);
	////lprintfln("itemCount: %d", itemCount);

	int nextItem = selectedIndex - 1;
	////lprintfln("nextItem: %d", selectedIndex);
	while(activePanels[nextItem] == 0)
	{
		nextItem--;		
		if(nextItem < 0) nextItem = itemCount - 1;
		////lprintfln("Don't select nextItem: %d", nextItem);
	}
	selectButton(nextItem);
}

void GridMenu::selectNextRow()
{
	int nextItem = (selectedIndex + _cols) % itemCount;
	while(activePanels[nextItem] == 0)
		nextItem = (nextItem + 1) % itemCount;

	selectButton(nextItem);
}

void GridMenu::selectPreviousRow()
{
	int nextItem = (selectedIndex + itemCount - _cols) % itemCount;
	while(activePanels[nextItem] == 0)
		nextItem = (nextItem + itemCount - 1) % itemCount;

	selectButton(nextItem);
}

void GridMenu::setSelectedIndex(int index)
{
	selectButton(index);
}

int GridMenu::getSelectedIndex()
{
	return selectedIndex;
}

void GridMenu::locateWidget(MAPoint2d point, bool inform)
{
	////lprintfln("Locating button");
	Point p;
	p.set(point.x, point.y);
	if(this->contains(p))
	{
		for(int i = 0; i < itemCount; i++)
		{
			////lprintfln("Testing item %d", i);
			if(getChildren()[i]->contains(p))
			{
				////lprintfln("Found button");
				selectButton(i);
				if(inform)
					informListener();
				break;
			}
		}
	}
}

void GridMenu::setEnabled(bool e)
{
	////lprintfln("Setting enablement");
	Environment& env = Environment::getEnvironment();
	if(!enabled && e)
	{
		env.addKeyListener(this);
		env.addPointerListener(this);
		////lprintfln("Enabled");
	}
	else if(enabled && !e)
	{
		env.removeKeyListener(this);
		env.removePointerListener(this);
		////lprintfln("Disabled");
	}
	
	enabled = e;	
}

void GridMenu::bindKeyDown(int keyCode)
{
	moveDownKeys.insert(keyCode);
}

void GridMenu::bindKeyLeft(int keyCode)
{
	moveLeftKeys.insert(keyCode);
}

void GridMenu::bindKeyRight(int keyCode)
{
	moveRightKeys.insert(keyCode);
}

void GridMenu::bindKeySelect(int keyCode)
{
	selectKeys.insert(keyCode);
}

void GridMenu::bindKeyUp(int keyCode)
{
	moveUpKeys.insert(keyCode);
}

bool GridMenu::setContains(Set<int> s, int value)
{
	bool found = false;
	for(Set<int>::Iterator itr = s.begin();itr != s.end();itr++)
	{
		if((*itr) == value)
		{
			found = true;
			break;
		}
	}

	return found;
}

void GridMenu::keyPressEvent(int keyCode)
{
}

void GridMenu::keyReleaseEvent(int keyCode)
{
	if(setContains(moveLeftKeys, keyCode))
		selectPrevious();
	if(setContains(moveRightKeys, keyCode))
		selectNext();
	if(setContains(moveUpKeys, keyCode))
		selectPreviousRow();
	if(setContains(moveDownKeys, keyCode))
		selectNextRow();
	if(setContains(selectKeys, keyCode))
		informListener();
}

void GridMenu::setPanelActive(int panelID, bool active)
{
	activePanels[panelID] = active == true ? 1 : 0;	
}
